<template>
    <div class="minW1060-auto bg-fff mt10">
        <div class="pad20 ml10">

            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/account' }">账号管理</el-breadcrumb-item>
                <el-breadcrumb-item>{{menuName}}</el-breadcrumb-item>
            </el-breadcrumb>

            <el-row>

                <el-col align="right">
                    <div class="dlblock-mb20 pr10 ml20">
                        <el-button type="primary" @click="submitForm('admin')">保存</el-button>
                    </div>
                    <div class="dlblock-mb20 pr10">
                        <el-button type="danger" @click="resetForm('admin')">重置</el-button>
                    </div>
                </el-col>

            </el-row>

            <el-row>
                <el-col align="left">
                    <h1>基本信息</h1>
                </el-col>
            </el-row>


            <el-row :gutter="30" class="mt20">
                <el-form label-position="left" :rules="rules" label-width="110px" :model="admin"
                         ref="admin">
                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="账号名称" prop="loginId">
                                <el-input v-model.trim="admin.loginId" :maxlength="20" show-word-limit></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="账号密码" prop="loginPwd">
                                <el-input v-model.trim="admin.loginPwd" type="password" @focus="checkPwd" :maxlength="20" show-word-limit></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="友好名称" prop="nickName">
                                <el-input v-model.trim="admin.nickName" :maxlength="20" show-word-limit></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" :offset="1">
                            <el-form-item label="性别">
                                <el-select v-model="admin.gender" clearable style="width: 100%">
                                    <el-option label="未知（保密）" :value="0"></el-option>
                                    <el-option label="男" :value="1"></el-option>
                                    <el-option label="女" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>

                    <el-row v-if="show">
                        <el-col :span="6" :offset="1">
                            <el-form-item label="账号状态" prop="state" label-width="110px">
                                <el-radio-group v-model="admin.state">
                                    <el-radio :label="0">启用账号</el-radio>
                                    <el-radio :label="3">锁定账号</el-radio>
                                    <el-radio :label="4">禁用账号</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-row>

            <el-row class="mt10" v-if="show">
                <el-col align="left">
                    <h1>账号登录日志</h1>
                </el-col>
            </el-row>
            <!--表格-->
            <el-row class="mt20" v-if="show">
                <el-table
                        height="350"
                        class="my-table"
                        :data="adminLogsList"
                        v-loading="loading"
                        :header-row-class-name="'my-table-header'"
                        ref="myTable"
                        border>
                    <el-table-column type="index" align="center" width="80%" label="序号"></el-table-column>
                    <el-table-column align="center" prop="loginId" label="账号名称"></el-table-column>
                    <el-table-column align="center" prop="speed" label="登录状态">
                        <template slot-scope="{row}">
                            <div v-if="row.state==0">登录成功</div>
                            <div v-else-if="row.state==1">用户名不存在</div>
                            <div v-else-if="row.state==2">密码错误</div>
                            <div v-else-if="row.state==3">非法客户端</div>
                            <div v-else-if="row.state==4">禁止登录</div>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="brithdayString" label="IP">
                        <template slot-scope="{row}">
                            <p>{{row.ip}}</p>
                        </template>
                    </el-table-column>

                    <el-table-column align="center" prop="brithdayString" label="客户端">
                        <template slot-scope="{row}">
                            <p>{{row.browser}}</p>
                            <p>{{row.system}}</p>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="time" label="账号登录时间"></el-table-column>
                </el-table>
            </el-row>
            <!--分页控件-->
            <el-row :gutter="20" v-if="show" class="mt20">
                <el-col :span="12" :offset="6" align="center">
                    <el-pagination background layout="total, prev, pager, next" :total="admin.total"
                                   :page-size="admin.size"
                                   :current-page="admin.page" @current-change="pageChange">
                    </el-pagination>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script>
    import {savaAdmin, getAdminLogsInfo} from '@/api';

    export default {
        name: 'index1',
        data() {

            const VerificationPwd = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'))
                } else if (value.length < 6) {
                    callback(new Error('密码不少于6位'))
                } else {
                    callback()
                }
            }
            return {
                //加载动画
                loading: false,
                //账号信息
                admin: {
                    loginId: '',
                    loginPwd: '',
                    nickName: '',
                    gender: '',
                    state: '',
                    disabled: '',
                    id: '',
                    page: 1,
                    size: 10,
                    total: 0
                },
                menuName: "",
                pastPwd: '',
                adminLogsList: [],
                show: false,
                //验证规则
                rules: {
                    loginId: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    loginPwd: [
                        {required: true, validator: VerificationPwd, trigger: 'blur'},
                    ],
                    disabled: [
                        {required: true, message: '请选择账号状态', trigger: 'blur'},
                    ],
                    state: [
                        {required: true, message: '请选择锁定状态', trigger: 'blur'},
                    ],
                },
            }
        },
        created() {
            if (this.$route.query.menuName != null) {
                this.menuName = this.$route.query.menuName
            }
        },

        methods: {

            checkPwd() {
                if (this.pastPwd == this.admin.loginPwd) {
                    this.admin.loginPwd = ''
                }
            },

            //提交表单
            submitForm(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.pastPwd == this.admin.loginPwd) {
                            this.admin.loginPwd = null
                        }
                        console.log(this.admin)
                        savaAdmin(this.admin).then(res => {
                            if (res.code == 200) {
                                this.$message.success("保存成功");
                                this.$router.push('/account')
                                this.$store.dispatch('tagsView/delView', {path: '/account/edit'})
                            }
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },

            //查询账号登录日志
            queryAdminLogs() {
                this.loading = true
                getAdminLogsInfo(this.admin).then(res => {
                    if (res != null && res.code == 200) {
                        this.adminLogsList = res.data.list;
                        this.admin.total = res.data.count
                    } else {
                        this.$message.info(res.message)
                    }
                }).finally(
                    this.loading = false
                )
            },
            // 分页查询
            pageChange(val) {
                this.admin.page = val
                this.queryAdminLogs()
            },
            //重置
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
        },
        mounted() {
            if (this.$route.query.admin != null) {
                this.admin = this.$route.query.admin
                this.pastPwd = this.admin.loginPwd
                this.show = true
            }
            this.queryAdminLogs()
        }
    }
</script>

<style scoped>

</style>
